/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbcdaos.ProductDao;
import jdbcdaos.PurchaseDao;
import jdbcdaos.UserDao;
import jdbcpojos.Purchase;
import jdbcpojos.PurchaseDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * Akcje zwiazane z zakupem produktow
 * @author piotrek
 */
@Controller
@RequestMapping(value="/jdbc")
public class PurchaseController {

	private PurchaseDao purchaseDao;
	private ProductDao productDao;
	private UserDao userDao;

	@Autowired
    public PurchaseController( PurchaseDao purchaseDao, ProductDao productDao, UserDao userDao) {
		this.purchaseDao	= purchaseDao;
		this.productDao		= productDao;
		this.userDao		= userDao;
	}

	/**
	 * Ok
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/{id}/purchase", method=RequestMethod.GET)
    protected String showPurchaseJdbc( @PathVariable("id") String id,
            Model model) throws Exception {
		
		model.addAttribute( "id", id );

		Purchase purchase = purchaseDao.getPurchase( Integer.parseInt( id ) );

		model.addAttribute( "purchase", purchase );

		return "purchase";
    }

	/**
	 * Zakup
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/purchase/add")
	protected String savePurchase(HttpServletRequest request,
            HttpServletResponse response) throws Exception{

		Purchase p = new Purchase();

		p.setCost( Math.random()*100 );
		p.setCustomer( userDao.getUser(458) );
		p.setPurchaseDate( new Date() );
		
		//przetwarzanie tablicy z danymi purchase details
		List<PurchaseDetail> purchaseDetails = new ArrayList<PurchaseDetail>();
		for(int i=0;i<3;i++){
			//petla
			PurchaseDetail pd = new PurchaseDetail();
			pd.setProduct( productDao.getProduct(3) );
			pd.setAmount( (int) (Math.random() * 100));
			purchaseDetails.add(pd);
		}
		p.setPurchaseDetails( purchaseDetails );

		purchaseDao.addPurchase( p );

		return "success";

	}

	/**
	 * Zakup z dostawa, uzycie bufora pierwszego poziomu
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/purchase/addWithShipment")
	protected String savePurchaseWithShipment(HttpServletRequest request,
            HttpServletResponse response) throws Exception{

		Purchase p = new Purchase();

		p.setCost( Math.random()*100 );
		p.setCustomer( userDao.getUser(458) );
		p.setPurchaseDate( new Date() );

		//przetwarzanie tablicy z danymi purchase details
		List<PurchaseDetail> purchaseDetails = new ArrayList<PurchaseDetail>();
		for(int i=0;i<3;i++){
			//petla
			PurchaseDetail pd = new PurchaseDetail();
			pd.setProduct( productDao.getProduct(3) );
			pd.setAmount( (int) (Math.random() * 100));
			purchaseDetails.add(pd);
		}
		p.setPurchaseDetails( purchaseDetails );

		purchaseDao.addPurchaseWithShipment( p );

		return "success";

	}

	@RequestMapping(value="/{id}/purchase/update")
    protected String updatePurchase( @PathVariable("id") String id,
            Model model) throws Exception {

		Purchase purchase = purchaseDao.getPurchase( Integer.parseInt( id ) );

		purchase.setCost( Math.random()*100 );

		/**
		 * Pobierz jedne z detali i zmien jedna z wartosci
		 */
		 for( PurchaseDetail pd: purchase.getPurchaseDetails() ){

			 pd.setAmount( (int)(Math.random()*100) );

		 }


		purchaseDao.updatePurchase(purchase);

		return "success";

	}

	@RequestMapping(value="/{id}/transactional/purchase/update")
    protected String updateTransactionalPurchase( @PathVariable("id") String id,
            Model model) throws Exception {

		Purchase purchase = purchaseDao.getPurchase( Integer.parseInt( id ) );

		purchase.setCost( Math.random()*100 );

		/**
		 * Pobierz jedne z detali i zmien jedna z wartosci
		 */
		 for( PurchaseDetail pd: purchase.getPurchaseDetails() ){

			 pd.setAmount( (int)(Math.random()*100) );

		 }


		purchaseDao.updateTransactionalPurchase(purchase);

		return "success";

	}

}